﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Diagnostics;

namespace LanguageConfigurationSolution
{
    /// <summary>
    /// Sets a label with a specific string value by retrieving the value of the configuration list with the given key (Key property)
    /// </summary>
    [ToolboxData("<{0}:ConfigurationHyperlink runat=server></{0}:ConfigurationHyperlink>")]
    public class ConfigurationHyperlink : HyperLink
    {

        public string TextKey
        {
            get;
            set;
        }

        public string TargetKey
        {
            get;
            set;
        }

        public string UrlKey
        {
            get;
            set;

        }

        public string ConfigurationlistName
        {
            get;
            set;
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            try
            {
                if (!string.IsNullOrEmpty(TargetKey))
                {
                    this.Target = ConfigurationReader.GetConfigurationValue(TargetKey, ConfigurationlistName, "");
                }
                if (!string.IsNullOrEmpty(TextKey))
                {
                    this.Text = ConfigurationReader.GetConfigurationValue(TextKey, ConfigurationlistName, "");
                }
                if (!string.IsNullOrEmpty(UrlKey))
                {
                    string tmp = ConfigurationReader.GetConfigurationValue(UrlKey, ConfigurationlistName, "");
                    if (!string.IsNullOrEmpty(tmp))
                    {
                        this.NavigateUrl = tmp;
                    }
                }
            }
            catch (Exception ex)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    EventLog.WriteEntry(
                        this.GetType().FullName,
                        string.Format("Error occured.{0}{1}", new string[] { Environment.NewLine, ex.ToString() }),
                        EventLogEntryType.Error);
                });

                throw ex;
            }
        }
    }
}
